package com.yuanfeng.goods.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.goods.entity.GoodsArticleUserFollowEntity;
import com.yuanfeng.goods.mapper.GoodsArticleInfoMapper;
import com.yuanfeng.goods.mapper.GoodsArticleUserFollowMapper;
import com.yuanfeng.goods.service.GoodsArticleUserFollowService;
import com.yuanfeng.goods.vo.CommunityUserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName GoodsArticleUserFollowServiceImpl
 * @Description  社区用户关注表
 * @Author cyp
 * @Date 2023-07-18 16:05:02
 * @Version 1.0
 **/
@Service
public class GoodsArticleUserFollowServiceImpl extends ServiceImpl<GoodsArticleUserFollowMapper, GoodsArticleUserFollowEntity>
        implements GoodsArticleUserFollowService {

    @Autowired
    private GoodsArticleInfoMapper goodsArticleInfoMapper;

    @Override
    public ResponseResult queryfollowList(Map<String, Object> param) {
        if (null == param.get("queryFlag")) {
            return ResponseResult.fail(0, "查询标识不能为空");
        }
        //查询标识: 1-查询我的关注;0-查询粉丝
        Integer queryFlag = (Integer) param.get("queryFlag");
        try {
            String token = (String) param.get("token");
            UserInfoVO rr = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
            if (rr == null) {
                return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
            } else {
                param.put("userId", rr.getUserId());
            }
            List<String> userIds = new ArrayList<>();
            List<CommunityUserInfoVO> data = new ArrayList<>();
            IPage<CommunityUserInfoVO> page = new Page<>();
            if (1 == queryFlag) {
                //我的关注
                page = this.baseMapper.getFollowUserIds(new Query<String>().getPage(param),param);
            } else if (0 == queryFlag) {
                //我的粉丝
                page = this.baseMapper.getFollowedUserIds(new Query<String>().getPage(param),param);
            }
            Map<Object,Object> tmpMap = new HashMap<>();
            for (CommunityUserInfoVO vo : page.getRecords()) {
                String userId = vo.getUserId();
                Map<String, String> map = goodsArticleInfoMapper.getUserInfo(userId);
                CommunityUserInfoVO userInfo = new CommunityUserInfoVO();
                userInfo.setUserId(userId);
                userInfo.setUserAvatar(map.get("userAvatar"));
                userInfo.setUserName(map.get("userName"));
                Integer flag = this.baseMapper.getFollowerFlag(userId,param.get("userId").toString());
                if(flag > 0){
                    userInfo.setIsFollowed(1);
                }else{
                    userInfo.setIsFollowed(0);
                }
                tmpMap.put("userId",userId);
                Integer followedNum = this.baseMapper.getFollowedNum(tmpMap);
                userInfo.setFollowedNum(followedNum);
                data.add(userInfo);
            }
            page.setRecords(data);
            return ResponseResult.success(PageUtils.getPage(page));
        } catch (Exception e) {

        }
        return ResponseResult.fail(BizCodeEnume.SYSTEM_ERROR.getCode(),BizCodeEnume.SYSTEM_ERROR.getMsg());
    }

    @Override
    public ResponseResult followUser(Map<Object, Object> param) {
        UserInfoVO ui = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if(null == ui){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }
        param.put("followUserId", ui.getUserId());
        int followFlag = (int) param.get("followFlag");
        if (1 == followFlag) {
            Integer count = this.baseMapper.queryExistForFollow(param);
            if (count > 0) {
                return ResponseResult.fail(0, "请勿重复操作！");
            }
            this.baseMapper.insertFollow(param);
        } else {
            this.baseMapper.deleteFollow(param);
        }
        this.baseMapper.updateFollowNum(param);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult batchfollowUser(Map<Object, Object> param) {
        UserInfoVO ui = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if(null == ui){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }
        param.put("followUserId", ui.getUserId());

        int followFlag = (int) param.get("followFlag");
        ArrayList<String> followedUserIds = (ArrayList<String>)param.get("followedUserIds");
        for(String followedUserId:followedUserIds) {
            param.put("followedUserId",followedUserId);
            if (1 == followFlag) {
                Integer count = this.baseMapper.queryExistForFollow(param);
                if (count > 0) {
                    return ResponseResult.fail(0, "请勿重复操作！");
                }
                this.baseMapper.insertFollow(param);
            } else {
                this.baseMapper.deleteFollow(param);
            }
            this.baseMapper.updateFollowNum(param);

        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult checkUserInfo(Map<Object, Object> param) {
        UserInfoVO ui = ParseTokenUtils.getUserInfoByTokenNoIsExist(param.get("token").toString());
        String userId = null;
        if(null != ui){
            //当前登录用户的userId
             userId = ui.getUserId();
        }
        Map<String,Integer> resultMap = new HashMap<>();
        if( param.get("userId").equals(userId)){
            resultMap.put("isMe",1);
            resultMap.put("isFollowed",null);
        }else{
            resultMap.put("isMe",0);
            Integer followFlag = this.baseMapper.getFollowerFlag((String)param.get("userId"),userId);
            if(followFlag >0 ){
                resultMap.put("isFollowed",1);
            }else{
                resultMap.put("isFollowed",0);
            }
        }
        return ResponseResult.success(resultMap);
    }
}
